<template>
<div>
    <!-- 面包屑区域start -->
          <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>商品管理</el-breadcrumb-item>
          <el-breadcrumb-item>商品列表</el-breadcrumb-item>
          </el-breadcrumb>
    <!-- 面包屑区域end -->
    <!-- 卡片区域start -->
        <el-card>
              <el-row>
                  <el-col >
                      <el-button type="primary" @click="showCate">添加分类</el-button>
                  </el-col>
              </el-row>
          </el-card>
    <!-- 卡片区域end -->
    <!-- 主体内容区域start -->
            <tree-table :data="categoriesList" :columns="columns" border :selection-type="false" :expand-type="false" show-index index-text="#">
                <template slot="isok" slot-scope="scope">
                    <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:green"></i>
                    <i class="el-icon-error" v-else style="color:red"></i>
                </template>
                <template slot="rank" slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.cat_level===0">一级</el-tag>
                    <el-tag type="warning" v-else-if="scope.row.cat_level===1">二级</el-tag>
                    <el-tag type="danger" v-else>三级</el-tag>
                </template>
                <template slot="handle">
                   <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                   <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                </template>
            </tree-table>
    <!-- 主体内容区域end -->
    <!-- 分页区域start -->
        <el-pagination
      @size-change="cateChange"
      @current-change="cateCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3, 5, 10, 12]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    <!-- 分页区域end -->

    <!-- 添加分类start -->
         <el-dialog
        title="添加分类"
        :visible.sync="addDialogVisible"
        width="50%" @close="addDialogClose">
        <!--表单区域start -->
        <el-form :model="cateForm" :rules="cateFormRules" ref="cateFormRef" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
            <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>
        <!-- 父级分类 -->
            <el-form-item label="父级名称:">
                <el-cascader
                    v-model="selectedkeys"
                    :options="parentList"
                    :props="panentprops"
                    @change="panentChange" expand-trigger="hover" clearable change-on-select></el-cascader>
            </el-form-item>
        </el-form>
        <!--表单区域end -->
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCatesubmit">确 定</el-button>
        </span>
        </el-dialog>
    <!-- 添加分类end -->
</div>
</template>

<script>
export default {
  data () {
    return {
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 全部商品分类数据
      categoriesList: [],
      //   总数据条数
      total: 0,
      //   为table指定列的定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          //   类型为模板
          type: 'template',
          //   模板的名字
          template: 'isok'
        },
        {
          label: '排序',
          //   类型为模板
          type: 'template',
          //   模板的名字
          template: 'rank'
        },
        {
          label: '操作',
          //   类型为模板
          type: 'template',
          //   模板的名字
          template: 'handle'
        }
      ],
      addDialogVisible: false,
      //   添加分类表单数据对象
      cateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      //   添加分类表单的校验规则
      cateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      //   父级分类的列表
      parentList: [],
      //   指定级联配置对象
      panentprops: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      //   选中的父级的分类
      selectedkeys: []
    }
  },
  created () {
    this.getCategoriesList()
  },
  methods: {
    // 获取商品分类数据
    async getCategoriesList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      console.log(res)
      this.categoriesList = res.data.result
      this.total = res.data.total
    },
    // 监听Pagesize改变
    cateChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCategoriesList()
    },
    // 监听PageNum改变
    cateCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getCategoriesList()
    },
    // 点击添加分类展示对话框
    showCate () {
      this.parentCateList()
      this.addDialogVisible = true
    },
    // 获取父级分类的数据
    async  parentCateList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 3 } })
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类数据失败')
      }
      this.parentList = res.data
    },
    // 选中的父级的分类
    panentChange () {
      console.log(this.selectedkeys)
      if (this.selectedkeys.length > 0) {
        this.cateForm.cat_pid = this.selectedkeys[this.selectedkeys.length - 1]
        this.cateForm.cat_level = this.selectedkeys.length
      } else {
        this.cateForm.cat_pid = 0
        this.cateForm.cat_level = 0
      }
    },
    // 点击确定按钮 添加新的分类
    addCatesubmit () {
      console.log(this.cateForm)
      this.$refs.cateFormRef.validate(async vali => {
        if (!vali) return
        const { data: res } = await this.$http.post('categories', this.cateForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        this.getCategoriesList()
        this.addDialogVisible = false
      })
    },
    // 监听对话框关闭事件
    addDialogClose () {
      this.$refs.cateFormRef.resetFields()
      this.selectedkeys = []
      this.cateForm.cat_pid = 0
      this.cateForm.cat_level = 0
    }

  }
}
</script>

<style lang="less" scoped>
.el-cascader {
    width: 100%;
}
</style>
